﻿using System;
using System.Configuration;
using System.Data;
using System.Web.UI;
using System.Web.UI.MobileControls;
using KPIS.GERP.ESS.BLL;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.DAL;
using KPIS.DBM;
using System.Text;

namespace KPIS.GERP.GFMS.APP
{
    public partial class Master : System.Web.UI.MasterPage
    {
        public static string strServerName;
        B_MessageSend send = new B_MessageSend();
        D_SECURITY_Users user = new D_SECURITY_Users();
        D_HRM_PS_Employees employee = new D_HRM_PS_Employees();

        protected void Page_Load(object sender, EventArgs e)
        {
            //CheckSession();
            strServerName = Request.Url.Host + (ConfigurationSettings.AppSettings["SubSystem"].ToString().Length > 0 ? "/" + ConfigurationSettings.AppSettings["SubSystem"].ToString() : "");
            //cmdInbox.Attributes.Add("onClick", "return false;");
            lbNotification.Attributes.Add("onClick", "return false;");

            string strControlName = ContentPlaceHolder1.Page.ToString();

            if (strControlName == "ASP.app_ui_forms_login_aspx")
            {
                RadMenu1.Visible = false;
                RadMenu2.Visible = true;
                PanelDetail.Visible = false;
                //PanelDetailBG.Visible = false;
            }
            else
            {
                RadMenu1.Visible = true;
                RadMenu2.Visible = false;
                PanelDetail.Visible = true;
                //PanelDetailBG.Visible = true;
            }

            if (Session["UserName"] != null)
            {
                //Encoding iso1 = Encoding.GetEncoding("Windows-874");
                //Encoding iso2 = Encoding.GetEncoding("ISO-8859-1");
                
                // string strUserName = Session["UserName"].ToString(); <-- problem about encoding, use database load instead  

                //strUserName = iso1.GetString(iso2.GetBytes(strUserName));

                var emp = employee.GetInfo(Convert.ToInt32(Session["EmpSeq"]));
                Session["UserName"] = emp != null ? emp.full_name : Convert.ToString(Session["UserName"]);
                lblUserName.Text = Convert.ToString(Session["UserName"]);
                UpdateUnreadCount();
                //cmdInbox.OnClientClick = "openTaskLists('@ServerName', '@Seq')"
                //    .Replace("@ServerName", strServerName).Replace("@Seq", Session["EmpSeq"].ToString());
            }

            if (!IsPostBack)
            {
                imgHeader.Src = "~/App_UI/Images/PageViews/" + ConfigurationSettings.AppSettings["Header"].ToString();
                
                //switch (GetSystemName())
                //{
                //    case "DEP":
                //    default:
                //        imgHeader.Src = "~/App_UI/Images/PageViews/Header.jpg";
                //        break;
                //    case "ITPF":
                //        imgHeader.Src = "~/App_UI/Images/PageViews/Header_ITPF.jpg";
                //        break;
                //}
            } 
        }

        public System.Web.UI.WebControls.Panel PanelContent1 { get { return PanelContent; }set { PanelContent = value; } }

        public void CreatedMenu(string strUserSeq)
        {
            //HttpCookie cookie = new HttpCookie("UserDetail");
            RadMenu1.DataSource = user.GetMenuItemList(strUserSeq);
            RadMenu1.DataBind();
        }

        protected void UpdateUnreadCount()
        {
            int nUnreadCount = send.GetUnreadCount(Session["EmpSeq"].ToString());
            int nOldUnreadCount = Convert.ToInt32(Session["UnreadCount"]);

            if (nUnreadCount > nOldUnreadCount)
            {
                ShowNotification(nUnreadCount);
            }

            Session["UnreadCount"] = nUnreadCount;
            //lblInboxCount.Text = nUnreadCount.ToString();
        }

        protected void ShowNotification(int nUnreadCount)
        {
            lbNotification.Text = "คุณมีข้อความที่ยังไม่ได้อ่าน " + nUnreadCount.ToString();
            RadToolTip1.Show();
        }

        //protected void cmdInbox_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect(@"~\App_UI\Forms\TaskLists.aspx");
        //}

        protected void cmdBackToSelectSystem_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Redirect("//" + strServerName + "/" + ConfigurationSettings.AppSettings["SubSystemPrefix"] + "ESS/APP_UI/Forms/SelectSystem.aspx");
        }

        protected string GetSystemName()
        {
            IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

            try
            {
                string strResult = "";
                string sql = "select system_name"
                    + " from sys_system_controls";
                DataTable dt = new DataTable();
                dbManager.Open();
                dt = dbManager.ExecuteDataSet(CommandType.Text, sql).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    strResult = dt.Rows[0][0].ToString();
                }

                return strResult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        private void CheckSession()
        {
            if (Session["EmpSeq"] == null)
            {
                Response.Redirect("//" + strServerName + "/ESS/APP_UI/Forms/Login.aspx");
            }
        }

        protected void Unnamed_Disposed(object sender, EventArgs e)
        {

        }

        protected void Unnamed_Disposed1(object sender, EventArgs e)
        {

        }
    }
}
